﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class registration_step2 : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        setAttributes();
        if (!Page.IsPostBack)
        {
            LoadCompanyType(this.LangId);
            LoadTransportType(this.LangId);
            ddlTypeTransport.Attributes.Add("multiple", "multiple");
        }
    }

    private void LoadTransportType(int lang)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 7, " + lang + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                //ddlTypeTransport.Items.Add(new ListItem("", "0"));
                if (lang == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        if (dr["id"].ToString() != "40" && dr["id"].ToString() != "41" && dr["id"].ToString() != "42")
                            ddlTypeTransport.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        if (dr["parent_id"].ToString() != "40" && dr["parent_id"].ToString() != "41" && dr["parent_id"].ToString() != "42")
                            ddlTypeTransport.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadCompanyType(int lang)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 2, " + lang + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlCompanyType.Items.Add(new ListItem("", "0"));
                if (lang == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        ddlCompanyType.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        ddlCompanyType.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void setAttributes()
    {
        ddlCompanyType.Attributes.Add("validate", "true");
        tbTaxNumber.Attributes.Add("validate", "true");
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
}